import streamlit as st
import model.model as mm
from annotated_text import annotated_text
import data.data as dd
import base64
st.set_page_config(
    page_title="AI旅游助手",
    page_icon = "🐼"

)
# 全新的AI助手，专用负责旅游聊天
def main_bg(main_bg):
    main_bg_ext = "jpg"
    st.markdown(
        f"""
         <style>
         .stApp {{
             background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
             background-size: cover
         }}
         </style>
         """,
        unsafe_allow_html=True)
main_bg('login.jpg')
user_id = st.session_state.user_id  # 用户id就是某一个用户的唯一标识
username = st.session_state.username

st.image("https://www.emojiall.com/images/120/microsoft-teams/1f6b6-1f3fb-200d-2642-fe0f.png", width=80)




st.text('这是你的旅游活地图，可以回答你任何关于旅游的问题，请尽情使用吧！')



# streamlit的session_state缓存器进行处理 新数据覆盖旧数据的问题


my_list = [
    " ",
    [
        " ",
        ("介休有哪些景点?","","#faa"),
        " ",
    ],
[
        " ",
        ("运城哪里最好看?","","#8ef"),
        " ",
    ],
[
        " ",
        ("去忻州如何旅游?","","#fea"),
        " ",
    ],
[
        " ",
        ("大同博物馆如何到大同古城?","","#faf"),
        " ",
    ],
[
        " ",
        ("去西安大雁塔应该坐几号地铁?","","#afa"),
        " ",
    ],
[
        " ",
        ("北京故宫距离哪个景点最近?","","#faa"),
        " ",
    ],
[
        " ",
        ("吕梁在哪里?","","#8ef"),
        " ",
    ],

    ("去云南旅游应该做一些什么攻略？", "","#fea"),
    " ",
]

annotated_text(my_list)



input = st.chat_input("请输入你要咨询的旅游问题")
if input:
    with st.chat_message("user"):
        st.write(input)
    dd.add_chat_message_from_tlat(user_id,input, "user")
    result = mm.chain_ly_invoke({"context":input})
    with st.chat_message("assistant"):
        st.write(result)
    dd.add_chat_message_from_tlat(user_id,result, "user")
st.balloons()

col1,col2= st.columns([1,1])
with col1:
    bt3 = st.button("历史记录")
    if bt3:
        history = dd.query_message_by_user_id_from_tlat(user_id=user_id)
        if history:
            for msg in history:
                with st.chat_message(msg["role"]):
                    st.write(msg["message"])
        else:
            with st.chat_message("assistant"):
                st.write("你好，我是你的专属旅游助手，可以回答你的任何的旅游问题")

with col2:
    bt3 = st.button("创建一个新的会话")
    if bt3:
        st.switch_page("pages/Intelligent-selection-interface.py")